<template>
  <a-modal v-bind="getBindValue" @cancel="handleCancel">
    <!-- <template #closeIcon v-if="!$slots.closeIcon">
      <ModalClose
        :canFullscreen="getProps.canFullscreen"
        :fullScreen="fullScreenRef"
        @cancel="handleCancel"
        @fullscreen="handleFullScreen"
      />
    </template> -->

    <template #footer v-if="!$slots.footer">
      <ModalFooter v-bind="getBindValue" @ok="handleOk" @cancel="handleCancel">
        <template #[item]="data" v-for="item in Object.keys($slots)">
          <slot :name="item" v-bind="data || {}"></slot>
        </template>
      </ModalFooter>
    </template>

    <!-- <ModalWrapper
      :useWrapper="getProps.useWrapper"
      :footerOffset="wrapperFooterOffset"
      :fullScreen="fullScreenRef"
      ref="modalWrapperRef"
      :loading="getProps.loading"
      :loading-tip="getProps.loadingTip"
      :minHeight="getProps.minHeight"
      :height="getWrapperHeight"
      :open="open"
      :modalFooterHeight="footer !== undefined && !footer ? 0 : undefined"
      v-bind="omit(getProps.wrapperProps, 'open', 'height', 'modalFooterHeight')"
      @ext-height="handleExtHeight"
      @height-change="handleHeightChange"
    >
      <slot></slot>
    </ModalWrapper> -->
    <slot></slot>
    <template #[item]="data" v-for="item in Object.keys(omit($slots, 'default'))">
      <slot :name="item" v-bind="data || {}"></slot>
    </template>
  </a-modal>
</template>
<script lang="ts" setup>
  import type { ModalMethods, ModalProps } from './types/typing'

  import { deepMerge } from '@/utils'
  import { isFunction } from '@/utils/is'
  import { omit } from 'lodash-es'
  import { computed, getCurrentInstance, nextTick, ref, toRef, unref, watch, watchEffect } from 'vue'
  import ModalFooter from './components/ModalFooter.vue'
  import { useFullScreen } from './hooks/useModalFullScreen'
  import { basicProps } from './props'

  defineOptions({
    inheritAttrs: false
  })
  const props = defineProps(basicProps)
  const emit = defineEmits(['open-change', 'height-change', 'cancel', 'ok', 'register', 'update:open'])
  const attrs = useAttrs()
  const openRef = ref(false)
  const propsRef = ref<Partial<ModalProps> | null>(null)
  const modalWrapperRef = ref<any>(null)

  // modal   Bottom and top height
  const extHeightRef = ref(0)
  const modalMethods: ModalMethods = {
    setModalProps,
    emitOpen: undefined,
    redoModalHeight: () => {
      nextTick(() => {
        if (unref(modalWrapperRef)) {
          ;(unref(modalWrapperRef) as any).setModalHeight()
        }
      })
    }
  }

  const instance = getCurrentInstance()
  if (instance) {
    emit('register', modalMethods, instance.uid)
  }

  // Custom title component: get title
  const getMergeProps = computed((): Recordable => {
    return {
      ...props,
      ...(unref(propsRef) as any)
    }
  })

  const { /**handleFullScreen,*/ getWrapClassName, fullScreenRef } = useFullScreen({
    modalWrapperRef,
    extHeightRef,
    wrapClassName: toRef(getMergeProps.value, 'wrapClassName')
  })

  // modal component does not need title and origin buttons
  // const getProps = computed((): Recordable => {
  //   const opt = {
  //     ...unref(getMergeProps),
  //     open: unref(openRef),
  //     okButtonProps: undefined,
  //     cancelButtonProps: undefined,
  //     title: undefined
  //   }
  //   return {
  //     ...opt,
  //     wrapClassName: unref(getWrapClassName)
  //   }
  // })

  const getBindValue = computed((): Recordable => {
    return {
      ...attrs,
      ...unref(getMergeProps),
      open: unref(openRef),
      wrapClassName: unref(getWrapClassName)
    }
  })

  // const getWrapperHeight = computed(() => {
  //   if (unref(fullScreenRef)) return undefined
  //   return unref(getProps).height
  // })

  watchEffect(() => {
    openRef.value = !!props.open
    fullScreenRef.value = !!props.defaultFullscreen
  })

  watch(
    () => unref(openRef),
    (v) => {
      emit('open-change', v)
      emit('update:open', v)
      instance && modalMethods.emitOpen?.(v, instance.uid)
      nextTick(() => {
        if (props.scrollTop && v && unref(modalWrapperRef)) {
          ;(unref(modalWrapperRef) as any).scrollTop()
        }
      })
    },
    {
      immediate: false
    }
  )

  // 取消事件
  async function handleCancel(e: Event) {
    e?.stopPropagation()
    // 过滤自定义关闭按钮的空白区域
    if ((e.target as HTMLElement)?.classList?.contains('modal-close--custom')) return
    if (props.closeFunc && isFunction(props.closeFunc)) {
      const isClose: boolean = await props.closeFunc()
      openRef.value = !isClose
      return
    }

    openRef.value = false
    emit('cancel', e)
  }

  /**
   * @description: 设置modal参数
   */
  function setModalProps(props: Partial<ModalProps>): void {
    // Keep the last setModalProps
    propsRef.value = deepMerge(unref(propsRef) || ({} as any), props)
    if (Reflect.has(props, 'open')) {
      openRef.value = !!props.open
    }
    if (Reflect.has(props, 'defaultFullscreen')) {
      fullScreenRef.value = !!props.defaultFullscreen
    }
  }

  function handleOk(e: Event) {
    emit('ok', e)
  }

  // function handleHeightChange(height: string) {
  //   emit('height-change', height)
  // }

  // function handleExtHeight(height: number) {
  //   extHeightRef.value = height
  // }
</script>
